package com.system.controller;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.frame.ar.model.BJUIPager;
import com.frame.ar.vo.AjaxDone;
import com.frame.jfinal.controller.BaseController;
import com.frame.model.MiniPager;
import com.system.service.ManagerService;
import com.system.service.ResourceService;


/*******************************************
 * 作用描述：系统资源管理控制类
 *******************************************
 * @author LRC
 * @createDate 2015-9-13
 * @version v1.0 
 * @updateRecord 格式：时间，作者，修改说明
 *******************************************
 * 
 */
public class ResourceController extends BaseController {
	
	public static Logger log = Logger.getLogger(ResourceController.class);


	/*******************************************
	 * 功能描述：回收站管理
	 *******************************************
	 * @createDate 2015-9-13 by LRC 
	 * @updateRecord 格式：时间，作者，修改说明
	 *******************************************
	 * 
	 */
	public void trashMgr(){
		String action = getPara("action");
		if (action == null) {
			renderError(404);
			return;
		}
		if("mgr".equals(action)){
			render("trash.html");
			return;
		}
		try{
			switch (action) {
				case "trashTree"://查询菜单数据
					List<Map<String,Object>> datas = ResourceService.getTrashTree();
					renderJson(datas);
					break;
				default:
					renderError(404);
			}
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			this.actionResult(false,"系统出现异常");
		}
	}
	/*******************************************
	 * 功能描述：系统菜单管理 
	 *******************************************
	 * @createDate 2015-9-13 by LRC 
	 * @updateRecord 格式：时间，作者，修改说明
	 *******************************************
	 * 
	 */
	public void menuMgr(){
		String action = getPara("action");
		if (action == null) {
			renderError(404);
			return;
		}
		if("mgr".equals(action)){
			//跳转到菜单管理主界面
			render("menu.html");
			return;
		}
		boolean success = false;
		try{
			switch (action) {
				case "saveMenu":
					Integer mid = ResourceService.saveMenu(this);
					this.actionResult(mid==null ? false : true,mid+"");
					break;
				case "menuTree"://返回菜单树
					List<Map<String,Object>> menus = ResourceService.getMenuTree();
					renderJson(menus);
					break;
				case "moveMenu"://移动菜单
					success = ResourceService.moveMenuTree(this);
					this.actionResult(success);
					break;
				case "delMenu"://删除菜单
					success = ResourceService.delMenu(this.getParaToInt("menuId"));
					this.actionResult(success);
					break;
				default:
					break;
			}
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			this.actionResult(false,"系统出现异常");
		}
	}
	
	/*******************************************
	 * 功能描述：系统参数管理 
	 *******************************************
	 * @createDate 2015-9-13 by LRC 
	 * @updateRecord 格式：时间，作者，修改说明
	 *******************************************
	 * 
	 */
	public void argumentMgr(){
		String action = getPara("action");
		if (action == null) {
			renderError(404);
			return;
		}
		if("mgr".equals(action)){
			render("argument.html");
			return;
		}
		Object id = getPara("id");
		Object pid = getPara("superId");
		boolean success = false;
		try{
			switch (action) {
				case "showDataGridByPid"://通过参数父节点查询参数信息
					setAttr("superId", pid);
					render("argDataGrid.html");
					break;
				case "argTree"://返回参数树
					List<Map<String,Object>> argTreeData = ResourceService.getArgTree(id);
					renderJson(argTreeData);
					break;
				case "findByPage"://分页查询
					BJUIPager pager = new BJUIPager();
					pager.initQueryParams(this,"=","action");
					ResourceService.findArgByPage(pager);
					renderJson(pager.getPageData());
				default:
					break;
			}
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			this.actionResult(false,"系统出现异常");
		}
	}
	
	/*******************************************
	 * 功能描述：参数数据表格操作
	 *******************************************
	 * @createDate 2015-9-13 by LRC 
	 * @updateRecord 格式：时间，作者，修改说明
	 *******************************************
	 * 
	 */
	public void arugmentDataGridAction(){
		String action = getPara("action");
		AjaxDone done = new AjaxDone();
		boolean success = false;
		try{
			switch (action) {
				case "save"://保存操作
					success = ResourceService.saveDataGridArgument(this);
					break;
				case "del"://删除操作
					success = ResourceService.delDataGridArgument(this);
					break;
				default:
					break;
			}
			done.setStatusCode(success ? AjaxDone.OK : AjaxDone.ERROR);
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			done.setStatusCode(AjaxDone.ERROR);
		}
		renderJson(done);
	}
}
